﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KabiManagementWeb.Models;
namespace KabiManagementWeb.Controllers
{
    public class SalaryController : Controller
    {
        SalaryFacade facade = new SalaryFacade(DBUtils.DBType.Local);
        KBManagementDataContext context = DBUtils.GetContext(DBUtils.DBType.Local);
        //
        // GET: /Salary/

        public ActionResult Index()
        {

            SetDropdownList();
            return View();

        }


        private void SetDropdownList()
        {
            List<SelectListItem> items = new List<SelectListItem>();

            for (int i = 1; i <= 12; i++)
            {
                var item = new SelectListItem();
                item.Text = i.ToString();
                item.Value = i.ToString();
                items.Add(item);
            }
            ViewData["month"] = new SelectList(items, "Value", "Text");
            List<SelectListItem> years = new List<SelectListItem>();
            var p = from psa in context.Pay_Salaries group psa.PayYear by psa.PayYear into psagroup select new {key=psagroup.Key};
           
            ViewData["year"] = new SelectList(p, "key", "key",DateTime.Now.Year);
        }
        [HttpPost]
        public ActionResult Index(int month, int year)
        {
            SetDropdownList();
            ViewData["ViewSalary"] = facade.GetListPaySalary(month, year);
            ViewData["TotalAmount"] = facade.GetTotalAmount(month, year);
            ViewData["TotalTipMoney"] = facade.GetTotalTip(month, year);
            ViewData["TotalPenaltyMoney"] = facade.GetTotalPenalty(month, year);
            ViewData["TotalRewardMoney"] = facade.GetTotalReward(month, year);
            return View();
        }

        //
        // GET: /Salary/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Salary/Create

        public ActionResult Create()
        {
            List<SelectListItem> items = new List<SelectListItem>();

            for (int i = 1; i <= 12; i++)
            {
                var item = new SelectListItem();
                item.Text = i.ToString();
                item.Value = i.ToString();
                items.Add(item);
            }
            ViewData["month"] = new SelectList(items, "Value", "Text");
            return View();
        }

        //
        // POST: /Salary/Create

        [HttpPost]
        public ActionResult Create(int month, int year)
        {
            try
            {
                var ps = new List<Pay_Salary>();
                foreach (var item in context.Employees)
                {
                    if (item.Status.Value)
                    {
                        var paysalary = new Pay_Salary();
                        paysalary.EmployeeID = item.ID;
                        paysalary.CashAdvance = 0;
                        paysalary.TotalPenaltyMoney = 0;
                        paysalary.TotalRewardMoney = 0;
                        paysalary.TotalTipMoney = 0;
                        paysalary.TotalAmount = item.Salary;
                        paysalary.RetainMoney = item.Salary;
                        paysalary.PayMonth = month;
                        paysalary.PayYear = year;
                        paysalary.Status = false;
                        ps.Add(paysalary);
                    }
                }
                context.Pay_Salaries.InsertAllOnSubmit(ps);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return View();
            }
        }

        //
        // GET: /Salary/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Salary/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Salary/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Salary/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [HttpPost]
        public ActionResult Payment(int id)
        {
            try
            {
                // TODO: Add delete logic here
                var pm = context.Pay_Salaries.Single(x => x.PsId == id);
                pm.Status = true;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Bonus(int id)
        {
            var ps = context.Pay_Salaries.Single(x => x.PsId == id);
            ViewData["ps"] = ps;
            return View();
        }
        [HttpPost]
        public ActionResult Bonus(int id, Reward rw)
        {
            try
            {
                // TODO: Add delete logic here
                var pm = context.Pay_Salaries.Single(x => x.PsId == id);

                pm.Rewards.Add(rw);
                pm.TotalRewardMoney += rw.Amount;
                pm.RetainMoney += rw.Amount;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [HttpPost]
        public ActionResult AdvanceMoney(int id, Cash_Advance amount)
        {
            try
            {
                // TODO: Add delete logic here
                var pm = context.Pay_Salaries.Single(x => x.PsId == id);

                pm.Cash_Advances.Add(amount);
                pm.TotalRewardMoney += amount.Amount;
                pm.RetainMoney -= amount.Amount;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
